﻿using System;
using System.Reflection.Metadata;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Vendors;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购退货单命令实现
    /// </summary>
    public class GetPreturnorderHandler : IRequestHandler<GetPreturnorderCommand, APIResult<List<PreturnorderDTO>>>
    {
        private readonly IBaseRepository<Preturnorder> _preturnorder;
        private readonly IBaseRepository<MaterialSupplier> _materialSupplier;
        private readonly IBaseRepository<Material_BasicModel> _materialBasicModel;

        public GetPreturnorderHandler(IBaseRepository<Preturnorder> preturnorder, IBaseRepository<MaterialSupplier> materialSupplier, IBaseRepository<Material_BasicModel> materialBasicModel)
        {
            _preturnorder = preturnorder;
            _materialSupplier = materialSupplier;
            _materialBasicModel = materialBasicModel;
        }

        public Task<APIResult<List<PreturnorderDTO>>> Handle(GetPreturnorderCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PreturnorderDTO>> result=new APIResult<List<PreturnorderDTO>>();
            result.Code=APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            var list = from a in _materialSupplier.GetAll().Result
                       join b in _preturnorder.GetAll().Result on a.Supid equals b.Supid
                       //join c in _materialBasicModel.GetAll().Result on b.material_code equals c.material_code
                       select new PreturnorderDTO
                       {
                           Preturnorderid = b.Preturnorderid,
                           Copynumber = b.Copynumber,
                           InvoiceSettlementEntityId = b.InvoiceSettlementEntityId,
                           DocumentStuct = b.DocumentStuct,
                           Supid = b.Supid,
                           SupplierName = a.SupplierName,
                           ReturnsNum = b.ReturnsNum,
                           OutinNum = b.OutinNum,
                           CatchNum = b.CatchNum,
                           DocumentTime = b.DocumentTime,
                           BusinessStuct = b.BusinessStuct,
                           Documentid = b.Documentid,
                           Returnwarehouse = b.Returnwarehouse,
                           Reasonreturn = b.Reasonreturn,
                           Notereason = b.Notereason,                         
                           DocumentCode = b.DocumentCode,
                           DocumentState = b.DocumentState,
                           Caozuoyuan = b.Caozuoyuan,
                           Shenheyuan = b.Shenheyuan,
                           ShenheDate = b.ShenheDate,
                           material_code = b.material_code,
                           //material_name = c.material_name,
                           //material_id=b.material_id
                       };
            //var list = _preturnorder.GetAll().Result;
            //if (request.material_code!=0)
            //{
            //    list = list.Where(x => x.material_code == request.material_code);
            //}
            //if (!string.IsNullOrEmpty(request.material_name)){
            //    list = list.Where(x => x.material_name.Contains(request.material_name));
            //}
            if (request.BeginDate != null && request.BeginDate.ToString() != "")
            {
                list = list.Where(x => x.DocumentTime >= request.BeginDate);
            }
            if (request.EndDate != null && request.EndDate.ToString() != "")
            {
                list = list.Where(x => x.DocumentTime <= Convert.ToDateTime(request.EndDate).AddDays(1).AddMilliseconds(-1));
            }
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
